package com.dream52.pms.sys.controller;

import com.dream52.pms.base.BaseController;
import com.dream52.pms.sys.entity.SysUser;
import com.dream52.pms.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/sys/sysUser")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @GetMapping("/list")
    @ResponseBody
    public String getSysUserList(Integer pageNo,Integer pageSize) throws Exception{
        SysUser user = this.getUserSession();
        String where = "1 = 1";
        if(user != null){
            if(null==pageNo||pageNo.toString().length() < 1){
                List sysUserList = this.sysUserService.getListByWhere(where,"id ASC");
                this.result.put("sysUserList", sysUserList);
            }else{
                PageInfo pageInfo = this.sysUserService.getPageByWhere(where,pageNo, this.pageSize,"id ASC");
                this.result.put("pageInfo", pageInfo);
            }
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE,"请先登录");
    }
    @GetMapping("/get")
    @ResponseBody
    public String getSysUser(Integer id) throws Exception{
        SysUser user = this.getUserSession();
        SysUser b = new SysUser();
        if(user != null ){
            if(id!=null && id>0){
                b = this.sysUserService.getById(id);
            }
            this.result.put("bean", b);
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE,"请先登录");
    }
    @PostMapping("/save")
    @ResponseBody
    public String saveSysUser(SysUser entity) throws Exception{
        SysUser user = this.getUserSession();
        if (entity.getId() == null){
            this.sysUserService.save(entity);
        }else {
            this.sysUserService.update(entity);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }
    @PostMapping("/del")
    @ResponseBody
    public String delSysUser( Integer id) throws Exception{
        SysUser user = this.getUserSession();
        if (id != null&&id.toString().length()>0){
            this.sysUserService.deleteById(id);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }
}